package gui.reports.removed;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import core.CoreFacade;
import gui.common.*;

/**
 * Controller class for the removed items report view.
 */
public class RemovedReportController extends Controller implements
		IRemovedReportController {

	CoreFacade model;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the removed items report view
	 */
	public RemovedReportController(IView view) {
		super(view);
		model = CoreFacade.getInstance();
		construct();
		
	}

	//
	// Controller overrides
	//

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IRemovedReportView getView() {
		return (IRemovedReportView) super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		return;
		
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		Calendar cal = model.getLastReportDate();
		
		if(cal != null)
		{
			getView().setSinceLast(true);
			getView().setSinceLastValue(cal.getTime());
			getView().enableSinceDateValue(false);
			
		}
		else
		{	
			getView().enableSinceLast(false);
			getView().setSinceDate(true);
			getView().enableSinceDateValue(true);
		}
		
	}

	//
	// IExpiredReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * removed items report view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		
		if(getView().getSinceDate())
		{
			getView().enableSinceDateValue(true);
		}
		else
		{
			getView().enableSinceDateValue(false);
		}
		
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the removed items report view.
	 */
	@Override
	public void display() 
	{
		Calendar cal = GregorianCalendar.getInstance();
		if(getView().getSinceDate())
		{
			Date date = getView().getSinceDateValue();
//			date.setMinutes(0);
			cal.setTime(date);
			cal.set(Calendar.SECOND, 0);
			
		}
		else
		{
			cal.setTimeInMillis(model.getLastReportDate().getTimeInMillis());
		}
		String filename = model.createRemovedItemsReport(getView().getFormat().toString(), cal);
		
		try
		{
			java.awt.Desktop.getDesktop().open(new File(filename));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
	}

}

